package com.hualan.sfzyproject.service;


import com.hualan.sfzyproject.domain.Notice;
import com.hualan.sfzyproject.mapper.NoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    public List<Notice> selectByUserId(int userId, int pageNum, int pageSize) {
        List<Notice> allNotices = noticeMapper.selectByUserId(userId);
        int maxPage = (int) Math.ceil((double) allNotices.size() / pageSize);
        if (allNotices.isEmpty() || pageNum > maxPage) {
            return null;
        }
        int start = Math.max(0, (pageNum - 1) * pageSize);
        int end = Math.min(allNotices.size(), start + pageSize);
        return allNotices.subList(start, end);
    }

    public int insertAll(Notice record) {
        return noticeMapper.insertAll(record);
    }
}
